package com.yazi.mxz.job.common.tool;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * @author jiang mo_yq5
 * @since 2012-5-18
 * 
 */
public class Email {

	private static Session mailSession;
	private static Message mailMessage;

	public static void config(String host, String fromName, String fromEmail, String fromPwd,
			boolean auth) throws MessagingException {
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		if (auth) {
			props.put("mail.smtp.auth", "true");
		} else {
			props.put("mail.smtp.auth", "false");
		}
		props.put("mail.smtp.host", host);
		mailSession = Session.getInstance(props, new MyAuthenticator(fromName, fromPwd));
		mailMessage = new MimeMessage(mailSession);
		mailMessage.setFrom(new InternetAddress(fromEmail));
	}

	public static void setEmails(String[] toEmails) throws MessagingException {
		String addresses = "";
		for (String toEmail : toEmails) {
			addresses += (addresses == "" ? "" : ",") + toEmail;
		}
		setEmails(addresses);
	}
	
	public static void setEmails(String toEmails) throws MessagingException {
		mailMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmails));

	}

	public static void send(String subject, String content, String[] files, Date sendDate) throws MessagingException {
			mailMessage.setSubject(subject);
			Multipart mp = new MimeMultipart();
			BodyPart bp = new MimeBodyPart();
			bp.setContent(content, "text/html;charset=utf-8 ");
			mp.addBodyPart(bp);

			for (String file : files) {
				BodyPart fileBp = new MimeBodyPart();
				FileDataSource fileds = new FileDataSource(file);
				fileBp.setDataHandler(new DataHandler(fileds));
				fileBp.setFileName(fileds.getName());
				mp.addBodyPart(fileBp);
			}

			mailMessage.setContent(mp);
			mailMessage.setSentDate(sendDate);
			mailMessage.saveChanges();
			Transport.send(mailMessage);
			/*
			 * Transport transport = mailSession.getTransport("smtp");
			 * transport.
			 * connect((String)mailSession.getProperties().get("mail.smtp.host"
			 * ),fromName,fromPwd);
			 * transport.sendMessage(mailMessage,mailMessage
			 * .getRecipients(Message.RecipientType.TO)); transport.close();
			 */

	}

	public static void send(String subject, String content) throws MessagingException {
		send(subject, content, new String[] {}, new Date());
	}

	public static void send(String subject, String content, Date sendDate) throws MessagingException {
		send(subject, content, new String[] {}, sendDate);
	}

	public void send(String subject, String content, String[] files) throws MessagingException {
		send(subject, content, files, new Date());
	}

	static class MyAuthenticator extends Authenticator {
		String userName = "";
		String password = "";

		public MyAuthenticator() {

		}

		public MyAuthenticator(String userName, String password) {
			this.userName = userName;
			this.password = password;
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(userName, password);
		}
	}

}
